package com.zs.usc.configuration.common;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * http://localhost:port/druid/index.html 访问
 *
 * @author
 */


@Configuration
@ConditionalOnProperty(value = "druid.enabled", havingValue = "true", matchIfMissing = true)
public class DruidConfig {

    @Value("${spring.druid.allow:127.0.0.1}")
    //ip白名单（没有配置或者为空，则允许所有访问
    private String allow;
    @Value("${spring.druid.deny:192.0.0.1}")
    private String deny;
    //++监控页面登录用户名
    @Value("${spring.druid.loginUsername:admin}")
    private String loginUsername;
    //++监控页面登录密码
    @Value("${spring.druid.loginPassword:admin}")
    private String loginPassword;
    //禁用HTML页面上的“Rest All”功能
    @Value("${spring.druid.resetEnable:false}")
    private String resetEnable;

    @Value("${spring.druid.urlPatterns:/*}")
    private String urlPatterns;
    @Value("${spring.druid.exclusions:*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*}")
    private String exclusions;
    @Value("${spring.druid.order:10}")
    private Integer order;


    /**
     * 注册Servlet信息， 配置监控视图
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean DruidStatViewServle2() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),
                "/druid/*");
        // 添加初始化参数：initParams
        /** 白名单，如果不配置或value为空，则允许所有 */
        //servletRegistrationBean.addInitParameter("allow", allow);//默认"127.0.0.1,192.0.0.1"
        /** 黑名单，与白名单存在相同IP时，优先于白名单 */
        //servletRegistrationBean.addInitParameter("deny", deny);//默认192.0.0.1
        /** 用户名 */
        servletRegistrationBean.addInitParameter("loginUsername", loginUsername);//默认admin
        /** 密码 */
        servletRegistrationBean.addInitParameter("loginPassword", loginPassword);//默认admin
        /** 禁用页面上的“Reset All”功能 */
        servletRegistrationBean.addInitParameter("resetEnable", resetEnable);//默认false
        return servletRegistrationBean;
    }

    /**
     * 注册一个：WebStatFilter, 监控拦截器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean druidStatFilter2() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());

        /** 过滤规则 */
        filterRegistrationBean.addUrlPatterns(urlPatterns); //默认"/*"
        /** 忽略资源 */
        filterRegistrationBean.addInitParameter("exclusions", exclusions); //默认"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*"
        filterRegistrationBean.setOrder(order);//默认10
        return filterRegistrationBean;
    }

    public static void main(String[] args) throws Exception {
        String key = "1234";
        String value = "liwenlong";
        String enValue = ConfigTools.encrypt(value);
        System.out.println(enValue);
        String denValue = ConfigTools.decrypt(enValue);
        System.out.println(denValue);
    }

}
